package com.ruoyi.pay.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.pay.domain.PayInterfaceConfig;
import com.ruoyi.pay.domain.PayInterfaceDefine;
import com.ruoyi.pay.domain.PayOrder;
import com.ruoyi.pay.mapper.PayInterfaceConfigMapper;
import com.ruoyi.pay.mapper.PayInterfaceDefineMapper;
import com.ruoyi.pay.service.PayInterfaceDefineService;
import com.ruoyi.pay.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 支付接口定义管理类
 *支付方式
 * @author zhuxiao
 * @site
 * @date 2021-04-27 15:50
 */

@RestController
@RequestMapping("/api/payIfDefines")
public class PayInterfaceDefineController extends BaseController {

    @Resource
    private PayInterfaceDefineMapper payInterfaceDefineMapper;

    @Resource
    private PayInterfaceDefineService payInterfaceDefineService;

    @Resource
    private PayInterfaceConfigMapper payInterfaceConfigMapper;
    @Autowired private PayOrderService payOrderService;


    /**支付接口--列表
     * @Author: ZhuXiao
     * @Description: list
     * @Date: 15:51 2021/4/27
    */
    @GetMapping("/list")
    @SaCheckPermission("pay:payIfDefine:list")
    public TableDataInfo<PayInterfaceDefine> list(PayInterfaceDefine payInterfaceDefine, PageQuery pageQuery) {
        return payInterfaceDefineService.selectPayInterfaceList(payInterfaceDefine, pageQuery);
    }


    /**支付接口--详情
     * @Author: ZhuXiao
     * @Description: detail
     * @Date: 15:51 2021/4/27
    */
    @GetMapping("/{ifCode}")
    public R<PayInterfaceDefine> detail(@PathVariable("ifCode") String ifCode) {
        return R.ok(payInterfaceDefineMapper.selectById(ifCode));
    }


    /**支付接口--新增
     * @Author: ZhuXiao
     * @Description: add
     * @Date: 15:51 2021/4/27
    */
    @PostMapping("/add")
    @SaCheckPermission("pay:payIfDefine:add")
    public R<Void> add(@RequestBody PayInterfaceDefine payInterfaceDefine) {
        int insert = payInterfaceDefineMapper.insert(payInterfaceDefine);
        if (insert == 0) {
            return R.fail("创建失败");
        }
        return R.ok();
    }

    /**支付接口--更新
     * @Author: ZhuXiao
     * @Description: update
     * @Date: 15:51 2021/4/27
    */
    @PostMapping("/update")
    @SaCheckPermission("pay:payIfDefine:edit")
    public R<Void> update( @RequestBody PayInterfaceDefine payInterfaceDefine) {
        //不跟新icon字段
        payInterfaceDefine.setIcon(null);
       int i = payInterfaceDefineMapper.updateById(payInterfaceDefine);
        if (i == 0) {
            return R.fail("更新失败");
        }
        return R.ok();
    }

    /**支付接口--删除
     * @Author: ZhuXiao
     * @Description: delete
     * @Date: 15:52 2021/4/27
    */
    @SaCheckPermission("pay:payIfDefine:del")
    @DeleteMapping("/{ifCode}")
    public R<Void> delete(@PathVariable("ifCode") String ifCode) {

        //ifCode格式 1,2 转为数组
        String[] split = ifCode.split(",");
        for (String s : split) {
            // 校验该支付方式是否有服务商或商户配置参数或者已有订单
            Wrapper<PayInterfaceConfig> eq = PayInterfaceConfig.gw().eq(PayInterfaceConfig::getIfCode, s);
            if (payInterfaceConfigMapper.selectCount(eq) > 0
                    || payOrderService.count(PayOrder.gw().eq(PayOrder::getIfCode, s)) > 0) {
                throw new ServiceException("该支付接口已有服务商或商户配置参数或已发生交易，无法删除！");
                    }
        }
         List<String> strings = Arrays.asList(split);
         int i = payInterfaceDefineMapper.deleteBatchIds(strings);
        if (i == 0) {
            return R.fail("删除失败");
        }
        return R.ok();
    }

}
